package com.ruoyi.bmxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.BmxtConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bmxt.domain.BmLevel;
import com.ruoyi.bmxt.service.IBmLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 层次管理Controller
 * 
 * @author 魏建波
 * @date 2023-02-19
 */
@RestController
@RequestMapping("/bmxt/level")
public class BmLevelController extends BaseController
{
    @Autowired
    private IBmLevelService bmLevelService;

    /**
     * 查询层次管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmLevel bmLevel)
    {
        startPage();
        List<BmLevel> list = bmLevelService.selectBmLevelList(bmLevel);
        return getDataTable(list);
    }

    @GetMapping("/listAllLevel")
    public AjaxResult listAllLevel()
    {
        List<BmLevel> list = bmLevelService.selectAllBmLevelList();
        return success(list);
    }

    /**
     * 导出层次管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:export')")
    @Log(title = "层次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmLevel bmLevel)
    {
        List<BmLevel> list = bmLevelService.selectBmLevelList(bmLevel);
        ExcelUtil<BmLevel> util = new ExcelUtil<BmLevel>(BmLevel.class);
        util.exportExcel(response, list, "层次管理数据");
    }

    /**
     * 获取层次管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:query')")
    @GetMapping(value = "/{levelId}")
    public AjaxResult getInfo(@PathVariable("levelId") Long levelId)
    {
        return success(bmLevelService.selectBmLevelByLevelId(levelId));
    }

    /**
     * 新增层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:add')")
    @Log(title = "层次管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmLevel bmLevel)
    {
        // 验证是否唯一 同一个项目的名称不能重复出现
        if (BmxtConstants.NOT_UNIQUE.equals(bmLevelService.checkProjectLevelNameUnique(bmLevel)))
        {
            return error("添加失败，项目级别信息已存在");
        }
        return toAjax(bmLevelService.insertBmLevel(bmLevel));
    }

    /**
     * 修改层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:edit')")
    @Log(title = "层次管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmLevel bmLevel)
    {
        // 验证是否唯一 同一个项目的名称不能重复出现
        if (BmxtConstants.NOT_UNIQUE.equals(bmLevelService.checkProjectLevelNameUnique(bmLevel)))
        {
            return error("修改失败，项目级别信息已存在");
        }
        return toAjax(bmLevelService.updateBmLevel(bmLevel));
    }

    /**
     * 删除层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:level:remove')")
    @Log(title = "层次管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{levelIds}")
    public AjaxResult remove(@PathVariable Long[] levelIds)
    {
        return toAjax(bmLevelService.deleteBmLevelByLevelIds(levelIds));
    }
}
